﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Routing;
using Atacadao.Comum.DataAnnotation;

namespace Atacadao.Comum.Pesquisa
{

	[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
	public sealed class DropDownListAttribute : Attribute, ITemplate
	{


		private static string m_defaultTemplateName = "DropDownList";
		public bool Executado { get; set; }


		public DropDownListAttribute(string entidade, string dataTextField, string dataValueField, string optionLabel) : this(entidade, dataTextField, dataValueField, optionLabel, null, m_defaultTemplateName)
		{
		}

		public DropDownListAttribute(string entidade, string dataTextField, string dataValueField, string optionLabel, object htmlAttributes) :
            this(entidade, dataTextField, dataValueField,  optionLabel, htmlAttributes, m_defaultTemplateName)
		{
		}

       

        private DropDownListAttribute(string entidade, string dataTextField__4, string dataValueField__3, string optionLabel__5, object htmlAttributes__6, string templateName__1)
		{
			if (string.IsNullOrEmpty(templateName__1)) {
				throw new ArgumentException("Template name cannot be empty.");
			}



			m_Nome = templateName__1;
			m_entidade = entidade;
			m_DataValueField = dataValueField__3;
			m_DataTextField = dataTextField__4;
			m_OptionLabel = optionLabel__5;
			HtmlAttributes = new RouteValueDictionary(htmlAttributes__6);
		}


		public string Nome {
			get { return m_Nome; }
		}

		private string m_Nome;
		public string Entidade {
			get { return m_entidade; }
			private set { m_entidade = value; }
		}

		private string m_entidade;
		public string DataValueField {
			get { return m_DataValueField; }
			private set { m_DataValueField = value; }
		}


		private string m_DataValueField;
		public string DataTextField {
			get { return m_DataTextField; }
			private set { m_DataTextField = value; }
		}

		private string m_DataTextField;
		public string OptionLabel {
			get { return m_OptionLabel; }
			private set { m_OptionLabel = value; }
		}

		private string m_OptionLabel;
		public IDictionary<string, object> HtmlAttributes {
			get { return m_HtmlAttributes; }
			private set { m_HtmlAttributes = value; }
		}

		private IDictionary<string, object> m_HtmlAttributes;

		private string m_where;
		public string Where {
			get { return m_where; }
			set { m_where = value; }
		}


		private static ICustomTypeDescriptor GetTypeDescriptor(Type type)
		{
			return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
		}

		public object GetSelectedText(object model)
		{
			return GetPropertyValue(model, !string.IsNullOrEmpty(DataTextField) ? DataTextField : DataValueField);
		}

		private static object GetPropertyValue(object model, string propertyName)
		{
			if (model != null) {
				PropertyDescriptor property = GetTypeDescriptor(model.GetType()).GetProperties().Cast<PropertyDescriptor>().SingleOrDefault(p => string.Compare(p.Name, propertyName, StringComparison.OrdinalIgnoreCase) == 0);

				if (property != null) {
					return property.GetValue(model);
				}
			}

			return null;
		}
	}
}
